package model

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Phone    string `gorm:"type:char(11)" json:"phone"`
	Password string `gorm:"type:varchar(35)" json:"password"`
	Username string `gorm:"type:varchar(20)" json:"username"`
	Image    string `gorm:"type:varchar(225)" json:"image"`
}

// 用户注册
func (u *User) AddUser(db *gorm.DB) error {
	return db.Debug().Create(&u).Error
}

// 验证手机号
func (u *User) VeryPhone(db *gorm.DB, phone string) error {
	return db.Debug().Where("phone =?", phone).First(&u).Error
}

// 搜索
func (u *User) SearchUser(db *gorm.DB, phone string, username string) (list []*User, err error) {
	err = db.Debug().Where("phone like ? or username like ?", "%"+phone+"%", "%"+username+"%").Find(&list).Error
	return list, err
}
